$(document).ready(function () {
	//切换类型  普通、黄金、白金、钻石、口腔
	switch_status();
	function switch_status() {
		var recharge_status_general = $('.recharge_status_general');//普通
		var recharge_status_gold = $('.recharge_status_gold');//黄金
		var recharge_status_platina = $('.recharge_status_platina');//白金
		var recharge_status_jewel = $('.recharge_status_jewel');//钻石
		var recharge_status_mouth = $('.recharge_status_mouth');//口腔
		var select_project = $('#select_project');//项目  商品
		$('.recharge_status').delegate('.text-center','click',function () {
			var span = $(this).find('span');
			$('.recharge_status').find('span').removeClass('active');//删除所有活动类
			span.addClass('active');//当前点击项添加活动类

			if(span.text() == '普通项目'){
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_general.addClass('active');//展示普通
				select_project.removeClass('active');
			}

			if(span.text() == '黄金项目'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_gold.addClass('active');//展示黄金
				select_project.addClass('active');
			}

			if(span.text() == '白金项目'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_platina.addClass('active');//展示隐藏白金
				select_project.addClass('active');
			}

			if(span.text() == '钻石项目'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_jewel.addClass('active');//展示钻石
				select_project.addClass('active');
			}

			if(span.text() == '口腔项目'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.addClass('active');//展示口腔
				select_project.addClass('active');
			}

		});
	}

	//设置滑动列表高度  超出滑动
	set_height_overflow('jollyone_general');
	set_height_overflow('experience_general');
	set_height_overflow('recharge_status_general');
	set_height_overflow('recharge_status_gold');
	set_height_overflow('recharge_status_platina');
	set_height_overflow('recharge_status_jewel');
	set_height_overflow('recharge_status_mouth');
	function set_height_overflow(contentId) {
		var con = $('#' + contentId);//超出滑动容器
		var recharge_status = $('.recharge_status');
		var nav = $('.nav');
		var select_project = $('#select_project');

		if( contentId == 'jollyone_general' || contentId == 'recharge_status_general' || contentId == 'experience_general' ){
			con.css('maxHeight', $(window).height() - recharge_status.height() - nav.outerHeight() - select_project.height() );
		}
		else {
			con.css('maxHeight', $(window).height() - recharge_status.height() - nav.outerHeight() );
		}
		con.css('overflow', 'auto' );
	}

	//选择产品类型
	select_jollyone_project();
	function select_jollyone_project() {
		var select = $('#select_project');
		var jollyone = $('#jollyone_general');
		var general = $('#recharge_status_general');
		var experience = $('#experience_general');

		select.delegate('.pull-left','click',function () {
			select.find('.active').removeClass('active');
			$(this).addClass('active');

			if( $(this).text() == '项目' ){
				jollyone.removeClass('active');
				experience.removeClass('active');
				general.addClass('active');
			}

			if( $(this).text() == '商品' ){
				general.removeClass('active');
				experience.removeClass('active');
				jollyone.addClass('active');
			}

			if( $(this).text() == '体验券' ){
				general.removeClass('active');
				jollyone.removeClass('active');
				experience.addClass('active');
			}
		});
	}

	//获取jollyone列表并展示
	get_pt_jollyone_list('jollyone_general');//普通项目
	function get_pt_jollyone_list(content) {
		$.ajax({
			url: '/Jolly/Jolly_list',
			type:'POST',
			dataType: 'json',
			data: {},
			success:function (data) {
				if(data.data){
					$('#' + content).empty();
					create_pt_jollyone_append_page( content , data.data );//jollyone列表生成并插入到页面
				}else {
					var html = '';
					html += '<div style="background-color: #f2f2f2;padding-top: 5rem;">';
					html += '<img style="width: 20%;margin-left: 40%" src="/statics/images/empty_project.png" alt="">';
					html += '<div class="text-center" style="font-size: 0.4rem;color: #999;">暂无项目</div>';
					html += '</div>';
					$('#' + content).html( html )
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}

	//jollyone列表生成并插入到页面
	function create_pt_jollyone_append_page( content ,data) {
		var general = $('#' + content);//jollyone项目列表
		for( var i = 0 , y = data.length ; i < y ; i++ ){
			var img = '';
			if( data[i].jolly_face_img == '' || data[i].jolly_face_img == null ){
				img = '/statics/images/u=4014792626,4066954880&fm=27&gp=0.jpg';
			}else {
				img = data[i].jolly_face_img;
			}

			var html = '';
			html += '<div>';
			html += '<div class="clearfix">';
			html += '<div class="pull-left"><img src="'+ img +'" alt=""></div>';
			html += '<div class="pull-left">';
			html += '<div>'+ data[i].jolly_name +'</div>';
			html += '<div>价值'+ data[i].jolly_price +'积分</div>';
			html += '</div>';
			html += '</div>';
			html += '<div class="clearfix look_detial">';
			html += '<div class="pull-right at_buy" project_name="'+ data[i].jolly_name +'" project_id="'+ data[i].jolly_id +'" score_type="'+ data[i].score_type +'" order_amount="'+ data[i].jolly_price +'">立即购买</div>';
			html += '<div class="pull-right look_detail" project_desc="'+ data[i].jolly_desc +'">查看详情</div>';
			html += '</div>';
			html += '<div style="height: 0.26666rem;background-color: #f2f2f2"></div>';
			html += '</div>';

			general.append( html );
		}
	}

	//获取项目列表并展示
	get_pt_project_list('experience_general','4');//体验券
	get_pt_project_list('recharge_status_general','1');//普通项目
	get_pt_project_list('recharge_status_gold','5');//黄金项目
	get_pt_project_list('recharge_status_platina','2');//白金项目
	get_pt_project_list('recharge_status_jewel','3');//钻石项目
	get_pt_project_list('recharge_status_mouth','6');//口腔项目
	function get_pt_project_list(content,score_type) {
		$.ajax({
			url: '/index/projectList',
			type:'GET',
			dataType: 'json',
			data: {
				score_type: score_type
			},
			success:function (data) {
				if(data.data){
					$('#' + content).empty();
					create_pt_list_append_page( content , data.data );//普通项目列表生成并插入到页面
				}else {
					var html = '';
					html += '<div style="background-color: #f2f2f2;padding-top: 5rem;">';
					html += '<img style="width: 20%;margin-left: 40%" src="/statics/images/empty_project.png" alt="">';
					html += '<div class="text-center" style="font-size: 0.4rem;color: #999;">暂无项目</div>';
					html += '</div>';
					$('#' + content).html( html )
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}

	//项目列表生成并插入到页面
	function create_pt_list_append_page( content ,data) {
		var general = $('#' + content);//普通项目列表
		for( var i = 0 , y = data.length ; i < y ; i++ ){
			var img = '';
			if( data[i].cover_img == '' || data[i].cover_img == null ){
				img = '/statics/images/u=4014792626,4066954880&fm=27&gp=0.jpg';
			}else {
				img = data[i].cover_img;
			}

			var html = '';
			if( i == ( y - 1 ) ){
				html += '<div>';
				html += '<div class="clearfix">';
				html += '<div class="pull-left"><img src="'+ img +'" alt=""></div>';
				html += '<div class="pull-left">';
				html += '<div>'+ data[i].project_name +'</div>';
				html += '<div>价值'+ data[i].project_price +'积分</div>';
				html += '</div>';
				html += '</div>';
				html += '<div class="clearfix look_detial">';
				html += '<div class="pull-right at_buy" project_name="'+ data[i].project_name +'" project_id="'+ data[i].project_id +'" score_type="'+ data[i].score_type +'" order_amount="'+ data[i].project_price +'">立即购买</div>';
				html += '<div class="pull-right look_detail" project_desc="'+ data[i].project_desc +'">查看详情</div>';
				html += '</div>';
				html += '</div>';
			}
			else {
				html += '<div>';
				html += '<div class="clearfix">';
				html += '<div class="pull-left"><img src="'+ img +'" alt=""></div>';
				html += '<div class="pull-left">';
				html += '<div>'+ data[i].project_name +'</div>';
				html += '<div>价值'+ data[i].project_price +'积分</div>';
				html += '</div>';
				html += '</div>';
				html += '<div class="clearfix look_detial">';
				html += '<div class="pull-right at_buy" project_name="'+ data[i].project_name +'" project_id="'+ data[i].project_id +'" score_type="'+ data[i].score_type +'" order_amount="'+ data[i].project_price +'">立即购买</div>';
				html += '<div class="pull-right look_detail" project_desc="'+ data[i].project_desc +'">查看详情</div>';
				html += '</div>';
				html += '<div style="height: 0.26666rem;background-color: #f2f2f2"></div>';
				html += '</div>';
			}

			general.append( html );
		}
	}

	//项目列表  点击查看详情  展示模态框
	click_look_detail('experience_general');//体验券
	click_look_detail('jollyone_general');//jollyone
	click_look_detail('recharge_status_general');//普通项目
	click_look_detail('recharge_status_gold');//黄金项目
	click_look_detail('recharge_status_platina');//白金项目
	click_look_detail('recharge_status_jewel');//钻石项目
	click_look_detail('recharge_status_mouth');//口腔项目
	function click_look_detail(recharge_status) {
		var general = $('#' + recharge_status);//普通项目列表
		general.delegate('.look_detail','click',function () {
			var self = $(this);
			var content = self.attr('project_desc').split(',');
			var html = '<div>项目介绍：</div>';

			$('#modal_detail').find('.modal-body').empty();
			$('#modal_detail').find('.modal-body').append( html );
			for( var i = 0 , y = content.length ; i < y ; i++ ){
				$('#modal_detail').find('.modal-body').append('<div>'+ content[i] +'</div>');
			}
			$('#modal_detail').modal();
		})
	}

	//项目列表  点击立即购买  展示模态框
	click_at_buy('experience_general');//体验券
	click_at_buy('jollyone_general');//jollyone
	click_at_buy('recharge_status_general');//普通项目
	click_at_buy('recharge_status_gold');//黄金项目
	click_at_buy('recharge_status_platina');//白金项目
	click_at_buy('recharge_status_jewel');//钻石项目
	click_at_buy('recharge_status_mouth');//口腔项目
	function click_at_buy(recharge_status) {
		var general = $('#' + recharge_status);//普通项目列表
		var select_project = $('#select_project');//选择商品  或者  项目
		general.delegate('.at_buy','click',function () {
			if( select_project.find('.active').text() == '项目' || select_project.find('.active').text() == '体验券' ){
				show_modal( $('#consume_modal') );//show  模态框

				show_content_at_buy( $(this) );//立即购买  模态框  内容展示
			}

			else{
				window.location = '/index/user_post_order?project_id=' + $(this).attr('project_id');
			}

		})
	}

	//立即购买  模态框  内容展示
	function show_content_at_buy($this) {
		//$this  指立即购买按钮
		var consume_modal = $('#consume_modal');//立即购买模态框

		var select_project = $('#select_project');//选择商品  或者  项目

		var status = '';
		if( $this.attr('score_type') == '1' || $this.attr('score_type') == '4' ){
			status = '普通项目';
		}
		if( $this.attr('score_type') == '5' ){
			status = '黄金项目';
		}
		if( $this.attr('score_type') == '2' ){
			status = '白金项目';
		}
		if( $this.attr('score_type') == '3' ){
			status = '钻石项目';
		}
		if( $this.attr('score_type') == '6' ){
			status = '口腔项目';
		}
		consume_modal.find('.form_project_name').text( $this.attr('project_name') );
		consume_modal.find('.form_value').text( $this.attr('order_amount') + '积分' );
		consume_modal.find('.form_project_status').text( status );

		show_modal( consume_modal );//show  模态框

		click_at_consume( $this );//点击购买模态框中立即购买按钮 发起请求 返回结果  ajax

	}

	//点击购买模态框中立即购买按钮 发起请求 返回结果  ajax
	function click_at_consume($this) {
		$('#at_consume').unbind('click');
		$('#at_consume').on('click',function () {
			var self = $(this);
			$.ajax({
				url: '/index/project_buy',
				type:'POST',
				dataType: 'json',
				data: {
					project_id: $this.attr('project_id'),
					score_type: $this.attr('score_type')
				},
				success:function (data) {
					var modal = $('#myModal');
					modal.find('.modal-body').text( data.msg + '!' );
					modal.modal({keyboard:false});

					modal.on('hide.bs.modal',function () {
						hide_modal( self.parents('.modal_define') );
						if( data.code == '1' ){
							window.location = '/index/user_order';//跳转订单页面
						}
					})

				},
				error:function (err) {
					console.log(err);
				}
			})
		})
	}

	/************************************模态框*************************************/
	//show  模态框
	function show_modal($modal) {
		if( !$modal.is(':animated') ){
			$('#mark').addClass('active');
			$modal.addClass('active');
			$modal.animate({
				top: $(window).height() - $modal.height(),
				bottom: $modal.height()
			},500);
		}
	}

	//hide  模态框
	function hide_modal($modal) {
		var font_size = $('html').attr('style').split(' ')[1].split('px')[0];
		var dpr = $('html').attr('data-dpr');
		$modal.animate({
			top: $(window).height() + ( font_size / dpr )
		},500,function () {
			$modal.removeClass('active');
			$('#mark').removeClass('active');
		});
	}

	//点击背景 或者.modal_close  关闭模态框
	click_hide_modal( $('.modal_close') );
	function click_hide_modal($btn) {
		$btn.on('click',function () {
			var modal = $(this).parents('.modal_define');
			hide_modal( modal );
		});
	}

	/************************************返回上一页***************************/
	//返回上一页
	function go_back() {
		history.go(-1);
	}

	//点击返回键  返回上一页
	click_back_btn();
	function click_back_btn() {
		$('#go_back').on('click',function () {
			go_back();//返回上一页
		});
	}
});